<template>
  <view class="container">
    <!-- 设备统计 -->
    <view class="stats-card">
      <view class="stat-item">
        <text class="stat-value">{{ totalDevices }}</text>
        <text class="stat-label">总设备数</text>
      </view>
      <view class="stat-item">
        <text class="stat-value">{{ onlineRate }}%</text>
        <text class="stat-label">在线率</text>
      </view>
      <view class="stat-item">
        <text class="stat-value">{{ abnormalCount }}</text>
        <text class="stat-label">异常设备</text>
      </view>
    </view>

    <!-- 设备分类导航 -->
    <scroll-view class="category-nav" scroll-x>
      <text v-for="(item, index) in categories" 
            :key="index" 
            :class="['nav-item', activeCategory === item.value ? 'active' : '']"
            @click="changeCategory(item.value)">
        {{ item.label }}
      </text>
    </scroll-view>

    <!-- 设备列表 -->
    <view class="device-list">
      <view v-for="(device, index) in filteredDevices" 
            :key="index" 
            class="device-card"
            @click="showDeviceDetail(device)">
        <view class="card-header">
          <text class="device-name">{{ device.name }}</text>
          <text :class="['status', device.status]">
            {{ statusMap[device.status] }}
          </text>
        </view>
        
        <view class="card-body">
          <view class="info-row">
            <uni-icons type="location" size="16" color="#666" />
            <text class="info-text">{{ device.location }}</text>
          </view>
          
          <view class="data-panel">
            <view class="data-item" v-if="device.temperature !== undefined">
              <text class="data-label">温度</text>
              <text class="data-value">{{ device.temperature }}℃</text>
            </view>
            <view class="data-item" v-if="device.humidity !== undefined">
              <text class="data-label">湿度</text>
              <text class="data-value">{{ device.humidity }}%</text>
            </view>
            <view class="data-item">
              <text class="data-label">最后上报</text>
              <text class="data-value">{{ device.lastReport }}</text>
            </view>
          </view>
        </view>
        
        <view class="card-footer">
          <text class="device-type">{{ device.type }}</text>
          <text class="install-time">安装时间：{{ device.installDate }}</text>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="filteredDevices.length === 0" class="empty-tips">
      <image src="/static/device-empty.png" class="empty-img" />
      <text class="empty-text">当前分类暂无设备</text>
    </view>

    <!-- 操作按钮 -->
    <view class="action-btns">
      <view class="add-btn" @click="addDevice">
        <uni-icons type="plus" size="28" color="#fff" />
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';

// 设备分类
const categories = [
  { label: '全部', value: 'all' },
  { label: '环境监控', value: 'environment' },
  { label: '喂养设备', value: 'feeding' },
  { label: '清洁设备', value: 'cleaning' },
  { label: '其他设备', value: 'others' }
];

// 模拟数据
const devices = ref([
  {
    id: 1,
    name: '温湿度传感器#1',
    type: '环境监控',
    status: 'online',
    location: 'A区3号鹅舍',
    temperature: 26.5,
    humidity: 65,
    lastReport: '2分钟前',
    installDate: '2023-08-15'
  },
  {
    id: 2,
    name: '自动喂食机#2',
    type: '喂养设备',
    status: 'offline',
    location: 'B区1号鹅舍',
    lastReport: '1小时前',
    installDate: '2024-01-10'
  },
  {
    id: 3,
    name: '消毒风机#3',
    type: '清洁设备',
    status: 'abnormal',
    location: 'C区2号鹅舍',
    lastReport: '30分钟前',
    installDate: '2023-12-05'
  }
]);

// 状态映射
const statusMap = {
  online: '在线',
  offline: '离线',
  abnormal: '异常'
};

// 当前选中的分类
const activeCategory = ref('all');

// 计算属性
const filteredDevices = computed(() => {
  if (activeCategory.value === 'all') return devices.value;
  return devices.value.filter(d => d.type === activeCategory.value);
});

const totalDevices = computed(() => devices.value.length);
const onlineRate = computed(() => 
  Math.round((devices.value.filter(d => d.status === 'online').length / devices.value.length * 100)
));

const abnormalCount = computed(() => 
  devices.value.filter(d => d.status === 'abnormal').length
);

// 方法
const changeCategory = (category) => {
  activeCategory.value = category;
};

const showDeviceDetail = (device) => {
  uni.navigateTo({
    url: `/pages/devices/detail?id=${device.id}`
  });
};

const addDevice = () => {
  uni.navigateTo({
    url: '/pages/devices/add'
  });
};
</script>

<style scoped lang="scss">
.container {
  padding: 20rpx;
  background: #f8f8f8;
  min-height: 100vh;
}

.stats-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  display: flex;
  justify-content: space-around;
  margin-bottom: 30rpx;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);

  .stat-item {
    text-align: center;
    flex: 1;
    
    .stat-value {
      font-size: 40rpx;
      color: #333;
      font-weight: 600;
      display: block;
    }
    
    .stat-label {
      font-size: 24rpx;
      color: #666;
    }
  }
}

.category-nav {
  white-space: nowrap;
  margin-bottom: 30rpx;
  padding: 0 20rpx;
  
  .nav-item {
    display: inline-block;
    padding: 16rpx 32rpx;
    margin-right: 20rpx;
    border-radius: 40rpx;
    background: #f0f0f0;
    color: #666;
    font-size: 28rpx;
    
    &.active {
      background: #4CAF50;
      color: #fff;
    }
  }
}

.device-list {
  .device-card {
    background: #fff;
    border-radius: 12rpx;
    margin-bottom: 24rpx;
    padding: 24rpx;
    box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20rpx;
      
      .device-name {
        font-size: 32rpx;
        color: #333;
        font-weight: 500;
      }
      
      .status {
        font-size: 26rpx;
        padding: 6rpx 16rpx;
        border-radius: 8rpx;
        
        &.online {
          background: #e8f5e9;
          color: #2e7d32;
        }
        
        &.offline {
          background: #f5f5f5;
          color: #666;
        }
        
        &.abnormal {
          background: #ffebee;
          color: #d32f2f;
        }
      }
    }

    .card-body {
      .info-row {
        display: flex;
        align-items: center;
        margin-bottom: 20rpx;
        
        .info-text {
          color: #666;
          font-size: 26rpx;
          margin-left: 10rpx;
        }
      }
      
      .data-panel {
        display: flex;
        justify-content: space-around;
        margin-top: 20rpx;
        
        .data-item {
          text-align: center;
          flex: 1;
          
          .data-label {
            display: block;
            color: #999;
            font-size: 24rpx;
          }
          
          .data-value {
            display: block;
            color: #333;
            font-size: 28rpx;
            margin-top: 8rpx;
          }
        }
      }
    }

    .card-footer {
      border-top: 1rpx solid #eee;
      margin-top: 20rpx;
      padding-top: 20rpx;
      display: flex;
      justify-content: space-between;
      color: #999;
      font-size: 24rpx;
    }
  }
}

.action-btns {
  position: fixed;
  right: 40rpx;
  bottom: 100rpx;
  
  .add-btn {
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
    background: #4CAF50;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8rpx 24rpx rgba(76,175,80,0.3);
  }
}

.empty-tips {
  text-align: center;
  padding: 100rpx 0;
  
  .empty-img {
    width: 300rpx;
    height: 300rpx;
    opacity: 0.6;
  }
  
  .empty-text {
    color: #999;
    font-size: 28rpx;
    margin-top: 20rpx;
  }
}
</style>